
#include <stdlib.h>
#include <math.h>
#include <GLES/gl.h> /* For NDK*/
#include <assert.h>
#include <android/log.h>

#include "app.h"
#include "utils.h"
#include "jade.h"


static long sStartTick = 0;
static long sTick = 0;

static int  action_idx = 0;


//static Texture *texture = NULL;
static jade_model_t *model = NULL;



///////////////////////////////////////////////////////////////////////////////

// Called from the app framework.
void appInit(int width, int height)
{
    int a;
    jade_texture_t *texture = NULL; 

    jade_world_create (width, height, 0.5f, 0.5f, 0.5f, 0.0f);


    //triangle data
    GLfloat vertices0[9] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f
    };
    GLfloat vertices1[9] = {
        0.0f, -1.0f, -1.0f,
        0.0f, -1.0f, 1.0f,
        0.0f, 1.0f, 0.0f
    };
    GLubyte colors[12] = {
        255, 0, 0, 0,
        0, 255, 0, 0,
        0, 0, 255 ,0
    };



    model = jade_model_create (2);

    //init first triangle
    jade_model_set_vertices (model, vertices0, 9 * sizeof(GLfloat), 0);
    jade_model_set_colors (model, colors, 12 * sizeof(GLubyte), 0);
    jade_model_set_triangle_num (model, 1, 0);

    //init second triangle
    jade_model_set_vertices (model, vertices1, 9 * sizeof(GLfloat), 1);
    jade_model_set_colors (model, colors, 12 * sizeof(GLubyte), 1);
    jade_model_set_triangle_num (model, 1, 1);

///    model->setPosition(0.0f, 0.0f, -5.0f);
///    model->setScale(5.0f, 5.0f, 5.0f);

  
}


// Called from the app framework.
void appDeinit()
{
    if (model != NULL)
        jade_model_destroy (model);
}

// Called from the app framework.
/* The tick is current time in milliseconds, width and height
 * are the image dimensions to be rendered.
 */
void appRender(long tick, int width, int height)
{
    if (sStartTick == 0)
        sStartTick = tick;

    // Actual tick value is "blurred" a little bit.
    sTick = (sTick + tick - sStartTick) >> 1;


    // Prepare OpenGL ES for rendering of the frame.
    jade_world_prepare_render(width, height);

/*
    gluLookAt(0.0f, 0.0f, 30.0f,
	          0.0f, 0.0f, 0.0f,
	          0.0f, 1.0f, 0.0f);
*/

    jade_model_render (model);


}

void appSetModelTextureOffset (double x, double y, double z) {

}


void appSetModelRotate (double x, double y, double z) {

}
